package com.ruoyi.business.factory.llm;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.mapper.SysUserMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.user.UserException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户类型模板
 *
 * @author 99
 * @interfaceName AbstractLLMTemplate
 * @description TODO
 * @date 2023/4/21 16:03
 */
@Slf4j
public abstract class AbstractUserTypeTemplate {

    /**
     * 注册具体用户时前置注册用户表
     *
     * @param phonenumber 手机号
     * @return
     */
    protected SysUser registerSmsBefore(String phonenumber) {
        SysUserMapper userMapper = SpringUtils.getBean(SysUserMapper.class);
        SysUser sysUser = userMapper.selectUserByPhonenumber(phonenumber);
        if (sysUser != null) {
            return sysUser;
        }
        // 注册
        SysUser user = new SysUser();
        user.setNickName("用户" + RandomUtil.randomString(8));
        user.setPhonenumber(phonenumber);
        user.setUserName(phonenumber);
        user.setStatus(UserStatus.OK.getCode());
        user.setCreateBy(null);
        user.setCreateTime(DateUtils.getNowDate());
        // user.setPassword(BCrypt.hashpw(user.getPassword()));
        user.setPassword(null);

        int insert = userMapper.insert(user);
        if (insert == 0) {
            log.info("登录用户：{} 注册失败.", phonenumber);
            throw new UserException("用户注册失败");
        }
        // todo 角色和部门确定了后台有了才写
        // 注册对应角色
        // 注册对应部门
        return user;
    }

    /**
     * 通过短信注册
     *
     * @param phonenumber 手机号
     * @return
     */
    public abstract Boolean registerSms(String phonenumber);

    /**
     * 通过手机号查询对应用户
     *
     * @param phonenumber 手机号
     * @return
     */
    public abstract SysUser selectUserByPhonenumber(String phonenumber);

}
